/*--------------------劳务模块.人员管理--ly-----------------*/
/*--------------------劳务人员信息修改功能 lb_people_edit.html------------------*/

$(document).ready(function() {
	//获取cookie:ueserid & Wtid
	var labourid = $.cookie("labourid");
	console.log(labourid);
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;    

	//填充数据
	var searchurl =  laborport + '/LabourController/selectById'; //查询
	$.post(searchurl, {
		'labourId': labourid,
	}, function(result) {
		var code = result.code;
		var editlist = result.object;
		var birthday = editlist.birthday;
		console.log(birthday);
		if(code == 0) {
			$("#L_name").val(editlist.name); //用户名
			$("#L_birthday").attr("value",birthday); //生日日期
			$("#L_age").val(editlist.age); //年龄
			$('#L_ismonitor').val(edit.ismonitor); //是否班长
			$("#L_gender").val(editlist.gender); //性别
			$("#L_idcard").val(editlist.idcard); //身份证号
			$("#L_presentaddr").val(editlist.presentaddr); //现居地址
			$("#L_familyaddr").val(editlist.familyaddr); //家庭地址
			$("#L_photourl").val(editlist.photourl); //照片
			$("#L_qrcodeurl").val(editlist.qrcodeurl); //二维码
		} else {
			console.log("网络不佳，稍后再试！")
		}
	});

	//layui
	layui.use(['form', 'layer'], function() {
		$ = layui.jquery;
		var form = layui.form,
			layer = layui.layer;

		//自定义验证规则
		form.verify({
			username: function(value) {
				if(value.length < 3) {
					return '昵称至少得3 个字符啊';
				}
			},
		});

		//获取表单数据
		$("input").blur(function() {
			//失去焦点事件
			var name = $("#L_name").val(); //用户名
			var birthday = $("#L_birthday").val(); //邮箱
			var age = $("#L_age").val(); //手机号码
			var gender = $("#L_gender").val(); //真实姓名
			var ismonitor = $('#L_ismonitor').val(); //是否班长
			var idcard = $("#L_idcard").val(); //微信公众号
			var presentaddr = $("#L_presentaddr").val(); //二维码地址
			var familyaddr = $("#L_familyaddr").val(); //密码
			var photourl = $("#L_photourl").val(); //密码
			var qrcodeurl = $("#L_qrcodeurl").val(); //密码
			//监听提交
			form.on('submit(edit)', function() {
				//递交修改
				editData(name, birthday, age, gender, ismonitor, idcard, presentaddr,familyaddr,photourl,qrcodeurl);
				return false;
			});
		});

		//修改用户信息
		var editurl = laborport + '/LabourController/update'; //修改
		// 获得frame索引
		var index = parent.layer.getFrameIndex(window.name);
		var editData = function(name, birthday, age, gender, ismonitor, idcard, presentaddr,familyaddr,photourl,qrcodeurl) {
			//修改 edit
			$.post(editurl, {
				labourid: labourid,
				name: name,
				birthday:birthday,
				age:age,
				gender:gender,
				ismonitor:ismonitor,
				idcard:idcard,
				presentaddr:presentaddr,
				familyaddr:familyaddr,
				photourl:photourl,
				qrcodeurl:qrcodeurl,
			}, function(result) {
				var code = result.code;
				if(code == 0) {
					layer.msg('修改成功!', {
						icon: 1,
						time: 1000
					}, function() {
						//关闭当前frame
						parent.layer.close(index);
					});
				} else {
					layer.msg('网络开小差了，请稍后再试!', {
						icon: 2,
						time: 1000
					}, function() {
						//关闭当前frame
//						parent.layer.close(index);
					});
				}
			})
		}

	});
})